package web.servlet;

import entity.MergeEntity;
import entity.PageEntity;
import entity.VegetablesTypes;
import service.Impl.VegetableImportImpl;
import service.Impl.VegetableTypeImpl;
import service.VegetableImportService;
import service.VegetableTypeService;
import web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/10.
 */
@WebServlet("/vegetableServlet/*")
public class VegetableImportServlet extends BaseServlet {
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //生成分页实体
        PageEntity pe=new PageEntity();
        //调用业务方法
        VegetableImportService vis=new VegetableImportImpl();
        try {
            //得到总记录数
            int sumCount=vis.rows();
            //将总记录数放入实体中
            pe.setrecordCounts(sumCount);
            //得到请求传过来的当前页
            String currentPage=request.getParameter("currentPage");
            currentPage=currentPage==null?"1":currentPage;
            //防止用户胡乱输
            int cp=0;
            try {
                cp=Integer.parseInt(currentPage);
            } catch (NumberFormatException e) {
                cp=1;
            }
            cp=cp<1?1:cp;
            cp=cp>pe.getPageCounts()?pe.getPageCounts():cp;
            //将正确的当前页放进model中
            pe.setPageIndex(cp);
            //将模型放进req范围中
            request.setAttribute("MODEL", pe);
            //调用分页业务方法
            List<MergeEntity> list=vis.queryAll(pe);
            //将list放进req范围内
           request.getSession().setAttribute("LIST",list);
            //请求转发到WEB-INF/view/show.jsp展示
            request.getRequestDispatcher("/WEB-INF/views/show.jsp").forward(request,response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //设置编码
        request.setCharacterEncoding("utf-8");
        //得到蔬菜订单编号
        String vegetableId=request.getParameter("vegetableid");
        VegetableImportService vis=new VegetableImportImpl();
        try {
            //调用业务方法
            int result=vis.delete(vegetableId);
            //设置响应内容类型
            response.setContentType("application/json;charset=utf-8");
            if(result==0){
                response.getWriter().write("");
                return;
            }
            response.sendRedirect("/vegetableServlet/query");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/querylist")
    public void querylist(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //设置编码
        request.setCharacterEncoding("utf-8");
        String vid=request.getParameter("id");
        //调用业务方法
        VegetableTypeService vts=new VegetableTypeImpl();
        try {
            List<VegetablesTypes> list=vts.query();
            //将list装到request中
            request.setAttribute("list",list);
            request.setAttribute("vid",vid);
            //请求转发到modify.jsp
            request.getRequestDispatcher("/WEB-INF/views/modify.jsp").forward(request,response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/modify")
    public void modify(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取参数
        String id=request.getParameter("importid");
        String kinds=request.getParameter("kinds");
        String quantity=request.getParameter("quantity");
        float q=Float.valueOf(quantity);
        int kind=Integer.valueOf(kinds);
        VegetableImportService vis=new VegetableImportImpl();
        try {
            //调用业务方法
            int r=vis.modify(id,q,kind);
            response.sendRedirect(request.getContextPath()+"/vegetableServlet/query");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
